﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using Persistence;
using Domain;
using Persistence.Dao;
namespace web
{
    public partial class cart : System.Web.UI.Page
    {
        protected List<OrderDetailMen> menlist = new List<OrderDetailMen>();
        protected List<OrderDetailKou> koulist = new List<OrderDetailKou>();
        protected List<OrderDetailOther> otherlist = new List<OrderDetailOther>();

        private void GetStoreDetail()
        {
            if (Session["OrderDetailMen"] != null)
                menlist = (List<OrderDetailMen>)Session["OrderDetailMen"];
            if(Session["OrderDetailKou"] != null)
                koulist = (List<OrderDetailKou>)Session["OrderDetailKou"];
            if (Session["OrderDetailOther"] != null)
                otherlist = (List<OrderDetailOther>)Session["OrderDetailOther"];
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            InitTable();
            GetStoreDetail();
            if (!IsPostBack)
            {
                BindList();
            }
            
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            bool bResult = false;
            System.Int32 ID = 0;
            if (!System.Int32.TryParse(e.CommandArgument.ToString(), out ID))
            {
                Message.alert("数据错误");
            }
            if (e.CommandName == "deleteOrderDetailMen")
            {
                for (int i = 0; i < menlist.Count; i++)
                {
                    if (menlist[i].id == ID)
                    {
                        menlist.Remove(menlist[i]);
                        bResult = true;
                    }
                }
            }
            if (e.CommandName == "deleteOrderDetailKou")
            {
                for (int i = 0; i < koulist.Count; i++)
                {
                    if (koulist[i].id == ID)
                    {
                        koulist.Remove(koulist[i]);
                        bResult = true;
                    }
                }
            }
            if (e.CommandName == "deleteOrderDetailOther")
            {
                for (int i = 0; i < otherlist.Count; i++)
                {
                    if (otherlist[i].id == ID)
                    {
                        otherlist.Remove(otherlist[i]);
                        bResult = true;
                    }
                }
            }
            if (bResult)
            {
                Message.AlertFresh("操作成功");
            }
            else
            {
                Message.alert("操作失败，请检查您的数据");
            }
        }


        private void InitTable()
        {
            tableMenEdit.Visible = false;
            tableKouEdit.Visible = false;
            tableOtherEdit.Visible = false;
            switch (rdTypeList.SelectedValue)
            {
                case "1":
                    tableMenEdit.Visible = true;
                    break;
                case "2":
                    tableKouEdit.Visible = true;
                    break;
                case "3":
                    tableOtherEdit.Visible = true;
                    break;                  
            }
        }

        protected void rdTypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitTable();
        }
        #region 
        
        #endregion
        protected void btnSubmitMen_Click(object sender, ImageClickEventArgs e)
        {
            bool bResult = false;
            switch (rdTypeList.SelectedValue)
            {
                case "1":
                    #region men
                    OrderDetailMen entity = new OrderDetailMen();
                    entity.id = menlist.Count;
                    entity.name = txt_name.Value.Trim();
                    entity.guiGeGao = Utils.ObjectToDecimal(txt_guiGeGao.Value.Trim(), 0);
                    entity.guiGeKuan = Utils.ObjectToDecimal(txt_guiGeKuan.Value.Trim(), 0);
                    entity.guiGeShen = Utils.ObjectToDecimal(txt_guiGeShen.Value.Trim(), 0);
                    entity.menXingHao = txt_menXingHao.Value.Trim();
                    entity.menJian = txt_menJian.Value.Trim();
                    entity.kaiQiFangXiang = ddlkaiqifangxiang.SelectedValue;
                    entity.danShuangKou = ddldanshuangkou.SelectedValue;
                    entity.jiCaiMen = ddljicaimen.SelectedValue;
                    entity.jiCaiKuangBan = ddljicaikuangban.SelectedValue;
                    entity.jiCaiKouXian = ddljicaikouxian.SelectedValue;
                    entity.kouXianXianXing = ddlkouxianxianxing.SelectedValue;
                    entity.hengKouXianXianKuan = Utils.ObjectToDecimal(txt_hengKouXianXianKuan.Value.Trim(), 0);
                    entity.hengKouXianShuLiang = Utils.ObjectToInt32(txt_hengKouXianShuLiang.Value.Trim(), 0);
                    entity.hengKouXian1XianKuan = Utils.ObjectToDecimal(txt_hengKouXian1XianKuan.Value.Trim(), 0);
                    entity.hengKouXian1ShuLiang = Utils.ObjectToInt32(txt_hengKouXian1ShuLiang.Value.Trim(), 0);
                    entity.liKouXianXianKuan = Utils.ObjectToDecimal(txt_liKouXianXianKuan.Value.Trim(), 0);
                    entity.likouxianshuliang = Utils.ObjectToInt32(txt_likouxianshuliang.Value.Trim(), 0);
                    entity.likouxian1xiankuan = Utils.ObjectToDecimal(txt_likouxian1xiankuan.Value.Trim(), 0);
                    entity.likouxian1shuliang = Utils.ObjectToInt32(txt_likouxian1shuliang.Value.Trim(), 0);
                    entity.likouxian2xiankuan = Utils.ObjectToDecimal(txt_likouxian2xiankuan.Value.Trim(), 0);
                    entity.likouxian2shuliang = Utils.ObjectToInt32(txt_likouxian2shuliang.Value.Trim(), 0);
                    entity.shangmao = ddlshangmao.SelectedValue;
                    entity.mupi = txt_mupi.Value.Trim();
                    entity.youqileixing = ddlyouqileixing.SelectedValue;
                    entity.youqiyansemen = ddlyouqiyansemen.SelectedValue;
                    entity.youqiyansekuangban = ddlyouqiyansekuangban.SelectedValue;
                    entity.youqiyansekouxian = ddlyouqiyansekouxian.SelectedValue;
                    entity.suoju = txt_suoju.Value.Trim();
                    entity.jiage = Utils.ObjectToDecimal(txt_jiage.Value.Trim(), 0);
                    entity.dinggoushuliang = Utils.ObjectToInt32(ddlMenJuzhong.SelectedValue, 0);
                    try
                    {
                        menlist.Add (entity);
                        Session["OrderDetailMen"] = menlist;
                        bResult = true;
                    }
                    catch { }
                    #endregion
                    break;
                case "2":
                    #region kou
                    OrderDetailKou entity_OrderDetailKou = new OrderDetailKou();
                    entity_OrderDetailKou.id = koulist.Count;
                    entity_OrderDetailKou.name = txt_OrderDetailKou_name.Value.Trim();
                    entity_OrderDetailKou.guigegao = Utils.ObjectToDecimal(txt_OrderDetailKou_guigegao.Value.Trim(), 0);
                    entity_OrderDetailKou.guigekuan = Utils.ObjectToDecimal(txt_OrderDetailKou_guigekuan.Value.Trim(), 0);
                    entity_OrderDetailKou.guigeshen = Utils.ObjectToDecimal(txt_OrderDetailKou_guigeshen.Value.Trim(), 0);
                    entity_OrderDetailKou.danshuangkou = kouddldanshuangkou.SelectedValue;
                    entity_OrderDetailKou.jicaikuangban = kouddljicaikuangban.SelectedValue;
                    entity_OrderDetailKou.jicaikouxian = kouddljicaikouxian.SelectedValue;
                    entity_OrderDetailKou.kouxianxianxing = kouddlkouxianxianxing.SelectedValue;
                    entity_OrderDetailKou.hengkouxianxiankuan = Utils.ObjectToDecimal(txt_OrderDetailKou_hengkouxianxiankuan.Value.Trim(), 0);
                    entity_OrderDetailKou.hengkouxianshuliang = Utils.ObjectToInt32(txt_OrderDetailKou_hengkouxianshuliang.Value.Trim(), 0);
                    entity_OrderDetailKou.hengkouxian1xiankuan = Utils.ObjectToDecimal(txt_OrderDetailKou_hengkouxian1xiankuan.Value.Trim(), 0);
                    entity_OrderDetailKou.hengkouxian1shuliang = Utils.ObjectToInt32(txt_OrderDetailKou_hengkouxian1shuliang.Value.Trim(), 0);
                    entity_OrderDetailKou.likouxianxiankuan = Utils.ObjectToDecimal(txt_OrderDetailKou_likouxianxiankuan.Value.Trim(), 0);
                    entity_OrderDetailKou.likouxianshuliang = Utils.ObjectToInt32(txt_OrderDetailKou_likouxianshuliang.Value.Trim(), 0);
                    entity_OrderDetailKou.likouxian1xiankuan = Utils.ObjectToDecimal(txt_OrderDetailKou_likouxian1xiankuan.Value.Trim(), 0);
                    entity_OrderDetailKou.likouxian1shuliang = Utils.ObjectToInt32(txt_OrderDetailKou_likouxian1shuliang.Value.Trim(), 0);
                    entity_OrderDetailKou.likouxian2xiankuan = Utils.ObjectToDecimal(txt_OrderDetailKou_likouxian2xiankuan.Value.Trim(), 0);
                    entity_OrderDetailKou.likouxian2shuliang = Utils.ObjectToInt32(txt_OrderDetailKou_likouxian2shuliang.Value.Trim(), 0);
                    entity_OrderDetailKou.shangmao = kouddlshangmao.SelectedValue;
                    entity_OrderDetailKou.mupi = txt_OrderDetailKou_mupi.Value.Trim();
                    entity_OrderDetailKou.youqileixing = kouddlyouqileixing.SelectedValue;
                    entity_OrderDetailKou.youqiyansekuangban = kouddlyouqiyansekuangban.SelectedValue;
                    entity_OrderDetailKou.youqiyansekouxian = kouddlyouqiyansekouxian.SelectedValue;
                    entity_OrderDetailKou.jiage = Utils.ObjectToDecimal(txt_OrderDetailKou_jiage.Value.Trim(), 0);
                    entity_OrderDetailKou.dinggoushuliang = Utils.ObjectToInt32(txt_OrderDetailKou_dinggoushuliang.Value.Trim(), 0);
                    
                    try
                    {
                        koulist.Add (entity_OrderDetailKou);
                        Session["OrderDetailKou"] = koulist;
                        bResult = true;
                    }
                    catch { }
                    
                    #endregion
                    break;
                case "3":
                    #region other
                    OrderDetailOther entity_OrderDetailOther = new OrderDetailOther();
                    entity_OrderDetailOther.id = otherlist.Count;
                    entity_OrderDetailOther.name = ddlOtherName.SelectedValue;
                    entity_OrderDetailOther.guige = txt_OrderDetailOther_guige.Value.Trim();
                    entity_OrderDetailOther.shuliang = Utils.ObjectToInt32(txt_OrderDetailOther_shuliang.Value.Trim(), 0);
                    entity_OrderDetailOther.guige1 = txt_OrderDetailOther_guige1.Value.Trim();
                    entity_OrderDetailOther.shuliang1 = Utils.ObjectToInt32(txt_OrderDetailOther_shuliang1.Value.Trim(), 0);
                    entity_OrderDetailOther.jicai = txt_OrderDetailOther_jicai.Value.Trim();
                    entity_OrderDetailOther.mupi = txt_OrderDetailOther_mupi.Value.Trim();
                    entity_OrderDetailOther.yanse = txt_OrderDetailOther_yanse.Value.Trim();
                    entity_OrderDetailOther.dinggoushuliang = Utils.ObjectToInt32(txt_OrderDetailOther_dinggoushuliang.Value.Trim(), 0);

                    try
                    {
                        otherlist.Add(entity_OrderDetailOther);
                        Session["OrderDetailOther"] = otherlist;
                        bResult = true;
                    }
                    catch { }
                    
                    #endregion
                    break;
            }
            if (bResult)
            {
                Message.AlertFresh("操作成功");
                BindList();
            }
            else
            {
                Message.alert("操作失败，请检查您的数据");
            }
        }


        private void BindList()
        {
            if (menlist.Count > 0)
            {
                rptList.Visible = true;
                rptList.DataSource = menlist;
                rptList.DataBind();
            }
            else
            {
                rptList.Visible = false;
            }
            if (koulist.Count > 0)
            {
                Repeater1.Visible = true;
                Repeater1.DataSource = koulist;
                Repeater1.DataBind();
            }
            else
            {
                Repeater1.Visible = false;
            }
            if (otherlist.Count > 0)
            {
                Repeater2.Visible = true;
                Repeater2.DataSource = otherlist;
                Repeater2.DataBind();
            }
            else
            {
                Repeater2.Visible = false;
            }
        }

        protected void btnSubmit_Click1(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/cartstep1.aspx");
        }
    }
}
